﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Data;
using CPPEI.Coolzon.Service.Contract;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Server.ServiceImplement
{
    /// <summary>
    /// 请求打开场景处理
    /// </summary>
    public class ReqOpenSceneHandler : BaseHandler<ReqOpenSceneData, RespOpenSceneData>
    {
        public ReqOpenSceneHandler()
            : base(RESPONSE_SCENARIO.SCENE, RESPONSE_COMMAND.SCENE.OPENSCENE)
        {
        }

        protected override RespOpenSceneData OnHandle(Data.MainServiceData.Request request, ReqOpenSceneData requestData)
        {
            CacheData.ControlDomain domain = CacheData.ServerData.Instance.GetDomain(request.Header.DomainId);
            CacheData.Client client = CacheData.ServerData.Instance.GetClient(request.Header.UserIdentity);
            if (domain != null && client != null)
            {
                if (client.State == CacheData.ClientState.SwitchSolution)
                {
                    // 方案切换之后控制端第一次打开场景，修改其他终端的状态为需要同步视图状态
                    List<CacheData.Client> clientList = domain.GetClientList();
                    for (int index = 0; index < clientList.Count; index++)
                    {
                        CacheData.Client memeberClient = clientList[index];
                        memeberClient.State = CacheData.ClientState.InDomain;
                    }
                    domain.UpdateViewState(domain.SolutionName, requestData.ViewStateAge, requestData.ViewStateToken);

                    RespOpenSceneData responseData = new RespOpenSceneData();
                    responseData.FrameName = requestData.FrameName;
                    responseData.ParameterString = requestData.ParameterString;
                    responseData.SceneName = requestData.SceneName;
                    responseData.SwitchMode = requestData.SwitchMode;

                    return responseData;
                }
                else if (client.State == CacheData.ClientState.InDomain)
                {
                    domain.UpdateViewState(domain.SolutionName, requestData.ViewStateAge, requestData.ViewStateToken);

                    RespOpenSceneData responseData = new RespOpenSceneData();
                    responseData.FrameName = requestData.FrameName;
                    responseData.ParameterString = requestData.ParameterString;
                    responseData.SceneName = requestData.SceneName;
                    responseData.SwitchMode = requestData.SwitchMode;

                    return responseData;
                }
                else
                {
                    // 状态不正确无法处理此操作
                    Common.Logger.WarningToTag("Handler", "收到了来自终端的打开场景操作，但是终端：{0}的状态不正确无法执行, 状态:{1}", request.Header.UserIdentity, client.State);
                }
            }
            return null;
        }
    }
}
